//
//  OSApplyView.m
//  ZKPayApp
//
//  Created by 凯朱 on 2024/10/30.
//

#import "OSApplyView.h"

@interface OSApplyView()<XZPickViewDelegate,XZPickViewDataSource>
@property (nonatomic, strong)NSMutableArray *banks;
@property (nonatomic, strong)XZPickView *bankPicker;
@property (nonatomic, strong)NSString *curBankName;
@end

@implementation OSApplyView
-(void)awakeFromNib{
    [super awakeFromNib];
    _sureBtn.layer.cornerRadius = 25;
    _sureBtn.layer.masksToBounds = YES;
    _midView.layer.cornerRadius = 10;
    _lbl1.text = kLocalString(@"借款天数");
    _lbl2.text = kLocalString(@"到账金额");
    _lbl3.text = kLocalString(@"应还金额");
    _lbl4.text = kLocalString(@"利息");
    _lbl5.text = kLocalString(@"服务费");
    _lbl6.text = kLocalString(@"还款日");
    _tiplbl1.text = kLocalString(@"收款信息");
    _tiplbl2.text = kLocalString(@"持卡人姓名：");
    _tiplbl3.text = kLocalString(@"收款银行名称：");
    _tiplbl4.text = kLocalString(@"收款银行卡号：");
    _tiplbl5.text = kLocalString(@"确认银行卡号：");
    _tf1.placeholder = kLocalString(@"请输入持卡人姓名");
    _tf2.placeholder = kLocalString(@"请选择收款银行");
    _tf3.placeholder = kLocalString(@"请输入银行卡号");
    _tf4.placeholder = kLocalString(@"请再次输入银行卡号");
    [_sureBtn setTitle:kLocalString(@"立即借款") forState:UIControlStateNormal];
    _topLbl1.text = kLocalString(@"贷款额度");
    
    _midView.layer.shadowColor = [UIColor colorWithRed:105/255.0 green:126/255.0 blue:196/255.0 alpha:0.16].CGColor;
    _midView.layer.shadowOffset = CGSizeMake(0,6);
    _midView.layer.shadowOpacity = 1;
    _midView.layer.shadowRadius = 16;
    
    _bottomView.layer.cornerRadius = 10;
    _bottomView.layer.shadowColor = [UIColor colorWithRed:105/255.0 green:126/255.0 blue:196/255.0 alpha:0.16].CGColor;
    _bottomView.layer.shadowOffset = CGSizeMake(0,6);
    _bottomView.layer.shadowOpacity = 1;
    _bottomView.layer.shadowRadius = 16;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        [self showBankView];
    }];
    _bgView2.userInteractionEnabled = YES;
    [_bgView2 addGestureRecognizer:tap];
    [self addTextLabel];
}

-(void)showBankView{
    if (_banks.count > 0) {
        [self configPickers];
        return;
    }
    [OSTool showHudOnWindow];
    [OSHttpManager postWithPath:@"sign/get_bank_list" params:nil finished:^(BOOL isSuccessed, id result, NSString *code, NSString *message) {
        [OSTool hideHudOnKeyWindow];
        if (!isSuccessed) {
            kShowToast(message);
            return;
        }
        self->_banks = [NSMutableArray new];
        NSArray *data = result[@"data"];
        [data enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSString *name = obj[@"bank_name"];
            [self->_banks addObject:name];
        }];
        [self configPickers];
    }];
}

-(void)configPickers{
    _bankPicker = [[XZPickView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight) title:kLocalString(@"选择银行")];
    _bankPicker.delegate = self;
    _bankPicker.dataSource = self;
    [_bankPicker show];
}

-(NSString *)pickerView:(XZPickView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return _banks[row];
}

-(NSInteger)numberOfComponentsInPickerView:(XZPickView *)pickerView{
    return 1;
}

-(NSInteger)pickerView:(XZPickView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return _banks.count;
}

- (void)pickView:(XZPickView *)pickerView confirmButtonClick:(UIButton *)button {
    NSInteger row = [pickerView selectedRowInComponent:0];
    _curBankName = _banks[row];
    _tf2.text = _curBankName;
}

/*
 添加富文本点击事件
 */
-(void)addTextLabel{
    
    NSString *textStr = kLocalString(@"请认真阅读并同意《借款协议》");
    
    __block NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:textStr];
    text.color = kHexColor(@"#A4B4EB");
    
    NSRange range = [textStr rangeOfString:kLocalString(@"《借款协议》")];
    [text setTextHighlightRange:range color:kColor_T backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull clickText, NSRange range, CGRect rect) {
        LTBaseWebViewController *vc = [LTBaseWebViewController new];
        vc.homeURL = @"https://www.baidu.com";
        [[OSTool getCurrentUIVC].navigationController pushViewController:vc animated:YES];
    }];
   
    YYLabel *textLabel = [YYLabel new];
    text.font = kFont(14);
    text.lineSpacing = 6;
    textLabel.numberOfLines = 0;
    textLabel.attributedText = text;
    //设置换行方式
    NSMutableParagraphStyle  *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
    paragraphStyle.lineSpacing = 6;
    text.paragraphStyle = paragraphStyle;
    
    //计算文本尺寸
    CGSize maxSize = CGSizeMake(KScreenWidth-40, MAXFLOAT);
    YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:maxSize text:text];
    textLabel.textLayout = layout;
    CGFloat introHeight = layout.textBoundingSize.height;
    textLabel.left = 0;
    textLabel.width = maxSize.width;
    textLabel.height = introHeight;
    textLabel.top = (25-introHeight)/2;
    [_readBgView addSubview:textLabel];
}



- (IBAction)readAction:(UIButton *)sender {
    sender.selected = !sender.selected;
}

- (IBAction)sureBtn:(id)sender {
    if (self.btnBlock) {
        self.btnBlock(1);
    }
}

@end
